package collection;

import java.util.*;

/**
 * 说明:
 *
 * @author: luohaowei
 * @date:2023/3/3
 **/
public class Sortpoint {
    public static void main(String[] args) {
        List<Point> list=new ArrayList<>();
        list.add(new Point(50,55));
        list.add(new Point(0,55));
        list.add(new Point(10,35));
        list.add(new Point(58,25));
        list.add(new Point(28,87));
        list.add(new Point(9,17));
        System.out.println("list的原始数据:"+list);
        list.sort(new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {
                return o1.getX()-o2.getX();
            }
        });
        System.out.println(list);
        list.sort((o1, o2) ->{
            int len1= o1.getX() *o1.getX()+o1.getY()*o1.getY();
            int len2=o2.getX() *o2.getX()+o2.getY()*o2.getY();
            return len1-len2;
        } );
        /*
        Collections.sort(list, new Comparator<Point>() {
            public int compare(Point o1, Point o2) {
                int len1= o1.getX() *o1.getX()+o1.getY()*o1.getY();
                int len2=o2.getX() *o2.getX()+o2.getY()*o2.getY();
                return len2-len1;
            }
        });
        System.out.println(list);

         */
        Collections.sort(list,(o1, o2) -> {
            int len1 = o1.getX() * o1.getX() + o1.getY() * o1.getY();
            int len2 = o2.getX() * o2.getX() + o2.getY() * o2.getY();

            return len1-len2;
        });
    }
}
